package shiro;

import com.tc.shiro.controller.LoginController;
import com.tc.shiro.controller.RoleController;
import com.tc.shiro.repository.PortalMenuRepository;
import com.tc.shiro.utils.SecurityUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.http.Cookie;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration  //可注入request
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "file:src/main/webapp/WEB-INF/mvc-servlet.xml"})
public class testShiroController {
    private MockMvc mockRoleController;
    private MockMvc mockLoginController;

    @Autowired
    private RoleController roleController;

    @Autowired
    private LoginController loginController;

    @Resource
    private Filter shiroFilter; //spring 管理的一些拦截器，注入到web.xml

    @Before
    public void setup() {
        this.mockRoleController = standaloneSetup(roleController).addFilter(shiroFilter).build();
        this.mockLoginController = standaloneSetup(loginController).addFilter(shiroFilter).build();
    }

    @Test
    public void demo1() throws Exception {
        MvcResult mvcResult = this.mockRoleController.perform(get("/shiro/queryRoleDetail")
                .param("roleNo", "999999999")
                .cookie(new Cookie("JSESSIONID","26b43b50-f673-48fa-809e-225b26d2bdfd"))
        )
                .andDo(print())
                .andExpect(status().isOk())
//                .andExpect(jsonPath("$.code").value(0))
                .andReturn();

    }

    @Test
    public void demo2() throws Exception {

        MvcResult mvcResult = this.mockLoginController.perform(post("/shiro/login")
                .param("loginName", "tc")
                .param("loginPwd", SecurityUtils.encrypt("12345678"))
                .param("verifyCode", "6053")
                .cookie(new Cookie("JSESSIONID","6782fdaa-9a6f-422a-ba06-e00c99b7866f"))
        )
                .andDo(print())
                .andExpect(status().isOk())
//                .andExpect(jsonPath("$.code").value(0))
                .andReturn();

    }




}
